package com.tongda.core.utils;

import java.util.concurrent.atomic.AtomicInteger;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MenuDBHelper extends SQLiteOpenHelper {

	public static MenuDBHelper instance = null;
	private AtomicInteger mOpenCounter = new AtomicInteger(); 
	/** 数据库名称 **/
	public static final String DATABASE_NAME = "canyin-menu-db.db";

	/** 数据库版本号 **/
	private static final int DATABASE_VERSION = 1;

	/** DB对象 **/
	SQLiteDatabase db = null;

	Context context = null;

	/** 菜肴表 建表SQL语句 **/
	public static final String DISH_TABLE_CREATE = "CREATE TABLE dish (id VARCHAR(36) PRIMARY KEY,categoryId VARCHAR(36),price INTEGER,name VARCHAR(32),dishescode VARCHAR(64),salenum INTEGER,unitname VARCHAR(32),isset VARCHAR(32),isRecommend VARCHAR(32),recommendDesc VARCHAR(128),isSpecialty VARCHAR(32),specialtyDesc VARCHAR(128),memberPrice VARCHAR(32),specialPrice VARCHAR(32),spiStr VARCHAR(256),url VARCHAR(256),lowurl VARCHAR(256),dsDishes VARCHAR(1024),dishesTasteStr VARCHAR(128),pungentLevel VARCHAR(32),isStopSell VARCHAR(32),isRulingPrice VARCHAR(32),dishesStyleStr VARCHAR(64),dishesMaterialStr VARCHAR(64),notes VARCHAR(256),estimate INTEGER,dishorderNum INTEGER,rulingPrice VARCHAR(36));";

	/** 菜类表 建表SQL语句 **/
	public static final String DISH_CATEGORY_TABLE_CREATE = "CREATE TABLE dish_category(id VARCHAR(36) PRIMARY KEY,name VARCHAR(32) NOT NULL,code VARCHAR(64), categoryLevel VARCHAR(64),parentCategoryId VARCHAR(64),dishNum INTEGER);";

	/** 购物车表 建表SQL语句 **/
	public static final String MENU_CART_TABLE_CREATE = "CREATE TABLE menu_cart(_id INTEGER PRIMARY KEY,id VARCHAR(36),categoryId VARCHAR(36),price INTEGER,name VARCHAR(32),dishescode VARCHAR(64),salenum INTEGER,unitname VARCHAR(32),isset VARCHAR(32),isRecommend VARCHAR(32),recommendDesc VARCHAR(128),isSpecialty VARCHAR(32),specialtyDesc VARCHAR(128),memberPrice VARCHAR(32),specialPrice VARCHAR(32),spiStr VARCHAR(256),url VARCHAR(256),dsDishes VARCHAR(1024),dishesTasteStr VARCHAR(128),pungentLevel VARCHAR(32),isStopSell VARCHAR(32),isRulingPrice VARCHAR(32),dishesStyleStr VARCHAR(64),dishesMaterialStr VARCHAR(64),tasteIdArray VARCHAR(64),tasteNameArray VARCHAR(64),avoidfoodIdArray VARCHAR(64),avoidfoodNameArray VARCHAR(64),ladu_value VARCHAR(32),billnotes VARCHAR(128),estimate INTEGER,ordersalenum INTEGER,dishesStatus VARCHAR(18),dinerBillDisheId VARCHAR(36),rulingPrice VARCHAR(36),isDinerDish VARCHAR(4) default ('0'));";

	public static synchronized MenuDBHelper getInstance(Context context) {
		if (instance == null) {
			synchronized (context) {
				instance = new MenuDBHelper(context);
			}
		}
		return instance;
	}

	public MenuDBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		synchronized (context) {
			// 得到数据库对象
			db = openDatabase();
			this.context = context;
		}
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DISH_TABLE_CREATE);
		db.execSQL(DISH_CATEGORY_TABLE_CREATE);
		db.execSQL(MENU_CART_TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}
	public synchronized SQLiteDatabase openDatabase() {  
        if(mOpenCounter.incrementAndGet() == 1) {  
        	db = getWritableDatabase();  
        }  
        return db;  
    }  
  
    public synchronized void closeDatabase() {  
        if(mOpenCounter.decrementAndGet() == 0) {  
        	db.close();  
        }  
    } 
}
